var baseUrl = require('../../utils/urllink.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,

    list: null,

    order_status_list: null,
    tab_index: 0,
    slider_offset: 0,
    slider_left: 0,
    slider_width: 0,
    order_list: [],
    page: 1,
    page_size:10,
    visible: false,
    shouchang_list:[],
    shouchang_show:false,
    guanzhu_list:[],
    guanzhu_show:true,
    hostimgs: app.globalData.hostimgs,
  },

  //点击状态刷新 0收藏1关注
  handleTab: function (event) {
    var that = this
    that.setData({
      slider_offset: event.currentTarget.offsetLeft,
      tab_index: event.currentTarget.id,
      page: 1,
      visible: true,
      shouchang_list: [],
      guanzhu_list: [],
    });

    if(this.data.tab_index == 0){
      that.setData({
        shouchang_show: false,
        guanzhu_show: true,
      })
      //收藏 
      getshouchang_list(that, app.globalData.user_id, that.data.page, that.data.page_size)

    }else{
      that.setData({
        shouchang_show: true,
        guanzhu_show: false,
      })
      //关注
      getguanzhu_list(that, app.globalData.user_id, that.data.page, that.data.page_size)

    }


  },

  //取消收藏
  qxshoucang:function(e){
    var that = this
    var goods_id = e.currentTarget.dataset.id;
    collect_goods_op(that, app.globalData.user_id, goods_id);//取消收藏
  },
  //取消关注
  qxguanzhu:function(e){
    var that = this
    var store_id = e.currentTarget.dataset.id;
    //var user_id = e.currentTarget.dataset.uid;
    collect_store_op(that, app.globalData.user_id, store_id)//取消关注
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    this.setData({
      slider_left: 0,
      slider_offset: this.data.window_width / 2 * this.data.tab_index,
      slider_width: this.data.window_width / 2
    });
    //收藏
    getshouchang_list(that, app.globalData.user_id, 1, that.data.page_size)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    //收藏 和关注
    if(that.data.tab_index == 0){
      getshouchang_list(that, app.globalData.user_id, that.data.page, that.data.page_size)
    }else{
      getguanzhu_list(that, app.globalData.user_id, that.data.page, that.data.page_size)
    }
    

  },

  // 收藏的 跳转到商品详细页面
  todetail:function(e){
    var that = this
    var goods_id = e.currentTarget.dataset.id;
    console.log(goods_id)
    wx.navigateTo({
      url: '../goods/detail?goods_id='+goods_id,
    })
  },
  // 关注的  跳转到店铺 (A的集合商品页面)
  tostore:function(e){
    var that = this;
    var store_id = e.currentTarget.dataset.id;
    var user_id = e.currentTarget.dataset.uid;
    wx.navigateTo({
      url: '../goods/goods_otherlist?store_id='+store_id+'&user_id='+user_id,
    })
  }
})

// 关注
function getguanzhu_list(that, user_id, page, page_size){
  wx.showLoading({
    title: '加载中...',
  })
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/collect_store.html',

    data: {
      user_id: user_id,
      page: page,
      page_size, page_size,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log("关注")
      console.log(res.data)

      if (res.data.code == -1 && page == 1) {
        that.setData({
          visible: false,
        })
      } else {
        if (page == 1) {
          //第一次
          that.setData({
            guanzhu_list: res.data,
            page: page + 1,
            visible: true,
          })
        } else {

          //分页
          var guanzhu_list = that.data.guanzhu_list;
          for (var i = 0; i < res.data.length; i++) {
            guanzhu_list = guanzhu_list.push(res.data[i])
          }

          that.setData({
            guanzhu_list: guanzhu_list,
            page: page + 1,
            visible: true,
          })
        }
        
      }
      wx.hideLoading();

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}
//收藏
function getshouchang_list(that, user_id, page, page_size) {
    wx.showLoading({
      title: '加载中...',
    })
    //收藏 
    //请求服务器
    wx.request({
      url: baseUrl +'weapp/Users/collect_goods.html',

      data: {
        user_id: user_id,
        page: page,
        page_size, page_size,
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/json'
      }, // 设置请求的 header
      success: function (res) {
        console.log("收藏")
        console.log(res.data)
        if (res.data.code == -1 && page == 1){
          that.setData({
            visible: false,
          })
        }else{
          if (page == 1) {
            //第一次
            that.setData({
              shouchang_list: res.data,
              page: page + 1,
              visible: true,
            })
          } else {

            //分页
            var shouchang_list = that.data.shouchang_list;
            for (var i = 0; i < res.data.length; i++) {
              shouchang_list = shouchang_list.push(res.data[i])
            }

            that.setData({
              shouchang_list: shouchang_list,
              page: page + 1,
              visible: true,
            })
          }

        }
        wx.hideLoading();


      },
      fail: function () {
      },
      complete: function () {
        // complete
      }
    })


}


//收藏/取消收藏
function collect_goods_op(that, user_id, goods_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/collect_goods_op.html',
    data: {
      user_id: user_id,
      goods_id: goods_id,

    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.type == 'del') {
        wx.showToast({
          title: '取消收藏',
        })
        that.setData({
          shouchang_list:[],
          page:1,
        })
        //收藏 和关注
        getshouchang_list(that, app.globalData.user_id, 1, that.data.page_size)
        
      } else {
        wx.showToast({
          title: '收藏成功',
        })

      }
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}

//取消关注接口：Users / collect_store_op
function collect_store_op(that, user_id, store_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/collect_store_op.html',
    data: {
      user_id: user_id,
      store_id: store_id,

    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.type == 'del') {
        wx.showToast({
          title: '取消关注',
        })
        that.setData({
          page:1,
          guanzhu_list:[],
        })
        getguanzhu_list(that, user_id, 1, that.data.page_size)
      } else {

      }
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })
}